// 输入框限制几位正小数
// 使用 --- v-Twodigit="2" //2 - - 两位小数
export const Twodigit = {
  mounted(el, binding) {
    const { value } = binding;
    //digit:1=一位小数，2=两位小数，10=0位小数能输入o 正数，11=0位小数不能输入o 正数
    const digit = value != null || value != undefined ? value : 2;
    const input = el.querySelector("input");
    input.setAttribute("type", "text");
    input.addEventListener("input", val => {
      const {
        target: { value }
      } = val;
      if (digit == 10) {
        val.target.value = value.replace(/^0+(\d)|[^\d]+/g, "");
      } else if (digit == 11) {
        val.target.value = value.replace(/^(0+)|[^\d]+/g, "");
      } else if (digit == 1) {
        val.target.value = value
          .replace(/^0[0-9]+/, val => val[1])
          .replace(/^(\.)+/, "")
          .replace(/[^\d.]/g, "")
          .replace(/\.+/, ".")
          .replace(/^(-)*(\d+)\.(\d).*$/, "$1$2.$3");
      } else {
        val.target.value = value
          .replace(/^0[0-9]+/, val => val[1])
          .replace(/^(\.)+/, "")
          .replace(/[^\d.]/g, "")
          .replace(/\.+/, ".")
          .replace(/^(-)*(\d+)\.(\d\d).*$/, "$1$2.$3");
      }
    });
    input.addEventListener("blur", val => {
      const values = val.target.value;
      if (values && values.indexOf(".") <= 0) {
        val.target.value = values + ".00";
      }
    });
  },
  unmounted(el) {
    const input = el.querySelector("input");
    input.addEventListener("input", () => {});
  }
};
